import Vue from 'vue'
import Router from 'vue-router'

// 仓库导入路由中
import store from "@/store"
// 导入element-ui
import { Message } from "element-ui"


Vue.use(Router)

let router =  new Router({
  routes: [
     {
       name:"login",
       path:"/login",
       component:()=>import("@/pages/Login.vue"),
       meta:{ title:"登录页面" }
     },
    {
      name: "layout",
      path: "/",
      component: () => import("@/pages/Layout.vue"),
      meta: { title: "后台管理系统" },
      redirect:"/home",
      children:[  // 所有的都是layout的子路由！
        { name: "home", path: "/home", component: () => import("@/pages/Home.vue"), meta: { title: "后台首页" } },
        //  菜单的路由 【列表、添加、修改】
        { name: "menu", path: "/menu", component: () => import("@/pages/Menu/Index"), meta: { title: "菜单管理" } },
        { name: "menuadd", path: "/menu/add", component: () => import("@/pages/Menu/Info"), meta: { title: "菜单添加" } },
        { name: "menuedit", path: "/menu/:id", component: () => import("@/pages/Menu/Info"), meta: { title: "菜单修改" } },
        //  角色的路由 【列表、添加、修改】
        { name: "role", path: "/role", component: () => import("@/pages/Role/Index"), meta: { title: "角色管理" } },
        { name: "roleadd", path: "/role/add", component: () => import("@/pages/Role/Info.vue"), meta: { title: "角色添加" } },
        { name: "roleedit", path: "/role/:id", component: () => import("@/pages/Role/Info.vue"), meta: { title: "角色修改" } },
        // 管理员的路由  【列表、添加、修改】
        { name: "admin", path: "/admin", component: () => import("@/pages/Admin/Index"), meta: { title: "管理员管理" } },
        { name: "adminadd", path: "/admin/add", component: () => import("@/pages/Admin/Info"), meta: { title: "管理员添加" } },
        { name: "adminedit", path: "/admin/:id", component: () => import("@/pages/Admin/Info"), meta: { title: "管理员修改" } },
        // 分类的路由 【列表、添加、修改】
        { name: "category", path: "/category", component: () => import("@/pages/Category/Index"), meta: { title: "商品分类管理" } },
        { name: "categoryadd", path: "/category/add", component: () => import("@/pages/Category/Info"), meta: { title: "商品分类添加" } },
        { name: "categoryedit", path: "/category/:id", component: () => import("@/pages/Category/Info"), meta: { title: "商品分类修改" } },
        // 属性的路由  【列表、添加、修改】
        { name: "specs", path: "/specs", component: () => import("@/pages/Specs/Index"), meta: { title: "商品规格管理" } },
        { name: "specsadd", path: "/specs/add", component: () => import("@/pages/Specs/Info"), meta: { title: "商品规格添加" } },
        { name: "specsedit", path: "/specs/:id", component: () => import("@/pages/Specs/Info"), meta: { title: "商品规格修改" } },
        // 商品的路由  【列表、添加、修改】
        { name: "goods", path: "/goods", component: () => import("@/pages/Goods/Index"), meta: { title: "商品管理" } },
        { name: "goodsadd", path: "/goods/add", component: () => import("@/pages/Goods/Info"), meta: { title: "商品添加" } },
        { name: "goodsedit", path: "/goods/:id", component: () => import("@/pages/Goods/Info"), meta: { title: "商品修改" } },
        // 会员的路由  【列表】
        { name: "member", path: "/member", component: () => import("@/pages/Member.vue"), meta: { title: "会员管理" } },
        // 轮播的路由  【列表、添加、修改】
        { name: "banner", path: "/banner", component: () => import("@/pages/Banner/Index"), meta: { title: "轮播图管理" } },
        { name: "banneradd", path: "/banner/add", component: () => import("@/pages/Banner/Info"), meta: { title: "轮播图添加" } },
        { name: "banneredit", path: "/banner/:id", component: () => import("@/pages/Banner/Info"), meta: { title: "轮播图修改" } },
        // 秒杀的路由  【列表、添加、修改】
        { name: "seckill", path: "/seckill", component: () => import("@/pages/Seckill/Index"), meta: { title: "秒杀管理" } },
        { name: "seckilladd", path: "/seckill/add", component: () => import("@/pages/Seckill/Info"), meta: { title: "秒杀添加" } },
        { name: "seckilledit", path: "/seckill/:id", component: () => import("@/pages/Seckill/Info"), meta: { title: "秒杀修改" } },
      ]
    }
  ]
})

// 导航守卫
router.beforeEach((to,from,next)=>{
  document.title = to.meta.title
  // 判断去的是不是登录页
  if(to.path=="/login"){
    next(); // 如果是登录页，直接去登录！
  } else { // 如果不是登录页  判断是否登录了
    if (localStorage.getItem("userinfo")) {    // 登录了，就要是否有权限！
      // 思路： 当前要访问的路径是否存在于当前管理员可以访问的接口地址里面
      // 判断to.path 是否在 管理员的 menus_url 里面
      // 把 / 和  /home 添加进去！
      let menus_url = store.getters.menus_url;
      menus_url = menus_url.concat(["/", "/home"])
      // 为了实现模糊路径可以进入     /menu/add 也可以进入。
      // 思路： 按照/分割，取第二个元素。再加上/ 和数组里面进行匹配
      let str = to.path.split("/")[1];
      str = "/"+str;
      if (menus_url.includes(str)) {
        next();
      } else {
        Message.error("您没有权限")
        router.push("/")
      }
    }else{   //没有登录，却直接进入内容页那么 跳转到登录页
      next("/login")
    }
  }

  // 第一层：判断是去登录页！
  //    第二层：判断是否有登录信息了！
  //        第三层：是否有权限！

})


export default router;
